//
//  LCComposePictureView.swift
//  Swift0319
//
//  Created by admin on 2017/3/29.
//  Copyright © 2017年 __MyCompanyName__. All rights reserved.
//

import UIKit
import SVProgressHUD

private let reuseIdentifier = "Cell"
// 计算cell的间距
let itemMargin: CGFloat = 5
//计算cell的宽高
let composePictureViewCellWH = (ScreenWidth - 2 * 10 - 2 * itemMargin) / 3

class LCComposePictureView: UICollectionView {

    var imageList:[UIImage] = [UIImage]()
    
    var closeEvent:(()->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        
        flowLayout.itemSize = CGSize(width: composePictureViewCellWH, height: composePictureViewCellWH)
        flowLayout.minimumLineSpacing = itemMargin
        flowLayout.minimumInteritemSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //warning
    func addImage(image: UIImage){
        isHidden = false
        // 添加到数组中
        if imageList.count == 3 {
            SVProgressHUD.showError(withStatus: "不能超过三张")
            return
//            imageList.removeLast()//原代码 , 替换掉第一张
        }
        imageList.append(image)
//        imageList.insert(image, at: 0)
        reloadData()
        
    }
    
    //FIXME:- 上午10:29 设置视图
    ///设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        
        register(LCComposePictureViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        dataSource = self
        
        delegate = self
    }

}

extension LCComposePictureView: UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = imageList.count
        return count + 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! LCComposePictureViewCell
        if indexPath.item == imageList.count {
            cell.image = nil
        }else{
            cell.image = imageList[indexPath.item]
            
            cell.closeEvent = { [weak self] in
                self?.imageList.remove(at: indexPath.item)
                
                self?.reloadData()
            }
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        collectionView.deselectItem(at: indexPath, animated: true)
        
        if indexPath.item == imageList.count {
            closeEvent?()
        }
    }
    
    
}



// 自定义LCComposePictureViewCell

class LCComposePictureViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var image: UIImage? {
        didSet{
            if image == nil {
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                closeButton.isHidden = true
            }else{
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                closeButton.isHidden = false
            }
        }
    }
    
    //FIXME:-告知删除对应的图片
    var closeEvent:(()->())?
    
    //FIXME:- 上午10:29 设置视图
    ///设置视图
    private func setupUI(){
        bgImageView.contentMode = .scaleAspectFill;
        bgImageView.clipsToBounds = true;
        contentView.addSubview(bgImageView)
        contentView.addSubview(closeButton)
        bgImageView.snp.makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        
        closeButton.snp.makeConstraints { (make) in
            make.top.right.equalTo(contentView)
        }
        
    }
    
    //FIXME:- 懒加载控件
    private lazy var bgImageView:UIImageView = UIImageView()
    
    lazy var closeButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(closeButtonClick), for: .touchUpInside)
        button.setImage(UIImage.init(named: "compose_photo_close"), for: .normal)
        return button
    }()
    
}


extension LCComposePictureViewCell {
    @objc fileprivate func closeButtonClick(){
        closeEvent?()
    }
}
